package com.pybox.service;

import com.pybox.common.utils.JdbcTemplateUtils;
import com.pybox.common.utils.ParamUtils;
import com.pybox.dto.TExportExcelDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author LeiYang
 * @title: TExportExceService
 * @projectName pybox
 * @description: 钥匙库存统计表
 * @date 2019/7/1615:01
 */

@Service
@Transactional
public class TExportExceService {

    @Autowired
    private JdbcTemplateUtils jdbcTemplateUtils;

    /**
     * 分页查询查询TCanbinetEventLogs
     *
     * @param pageNo
     * @param pageSize
     * @param paramMap
     * @return
     */
    public Page<TExportExcelDto> page(Integer pageNo, Integer pageSize, Map<String, String> paramMap) {

        Pageable pageable = new PageRequest(pageNo - 1, pageSize);

        List<Object> object = new ArrayList<>();
        StringBuffer sql = new StringBuffer("SELECT\n" +
                "\to.id as texportExcelId,\n" +
                "\to.`status`,\n" +
                "\to.`name` AS objName,\n" +
                "\to.`row`,\n" +
                "\to.col,\n" +
                "\to.pos,\n" +
                "\tc.`name` AS cabinetname\n" +
                "FROM\n" +
                "\tt_objects o\n" +
                "\tLEFT JOIN t_cabinets c ON o.cabinet_uid = c.uuid where 1 =1 ");
        StringBuffer sbf = new ParamUtils().getExportExcelParams(paramMap, sql);

        Page<TExportExcelDto> data = jdbcTemplateUtils.page(sbf, object, TExportExcelDto.class, pageNo, pageSize);

        Page<TExportExcelDto> page = new PageImpl<>(data.getContent(), pageable, data.getTotalElements());


        return page;
    }
}
